﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using CarparkIMS_Model;
using Microsoft.AspNetCore.Authorization;

namespace CarparkIMS_CMS.Controllers
{
    public class PlateRegistrationController : Controller
    {
        private readonly IMSContext _context;

        public PlateRegistrationController(IMSContext context)
        {
            _context = context;
        }
        [Authorize(AuthenticationSchemes = Startup.AuthSchemes)]
        // GET: PlateRegistration
        public async Task<IActionResult> Index(string searchString)
        {
            var query = _context.llqqPlateMapping.Where(a => !a.Deleted);
            if (!String.IsNullOrEmpty(searchString))
                query = query.Where(a => a.CardID.Contains(searchString) || a.PlateNo.Contains(searchString));
            return View(await query.ToListAsync());
        }
        [Authorize(AuthenticationSchemes = Startup.AuthSchemes)]
        // GET: PlateRegistration/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var llqqPlateMapping = await _context.llqqPlateMapping
                .FirstOrDefaultAsync(m => m.ID == id);
            if (llqqPlateMapping == null)
            {
                return NotFound();
            }

            return View(llqqPlateMapping);
        }
        [Authorize(AuthenticationSchemes = Startup.AuthSchemes)]
        // GET: PlateRegistration/Create
        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }

        // POST: PlateRegistration/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        public async Task<IActionResult> Create([Bind("UserID,CardID,PlateNo,Deleted")] llqqPlateMapping llqqPlateMapping)
        {
            if (_context.llqqPlateMapping.Where(a => a.PlateNo == llqqPlateMapping.PlateNo && !a.Deleted).Any())
            {
                ModelState.AddModelError("PlateNo", "Plate No duplicate");
            }
            if (_context.llqqPlateMapping.Where(a => a.CardID == llqqPlateMapping.CardID && !a.Deleted).Any())
            {
                ModelState.AddModelError("CardID", "CardID duplicate");
            }

            if (ModelState.IsValid)
            {
                _context.Add(llqqPlateMapping);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(llqqPlateMapping);
        }
        [Authorize(AuthenticationSchemes = Startup.AuthSchemes)]
        // GET: PlateRegistration/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var llqqPlateMapping = await _context.llqqPlateMapping.FindAsync(id);
            if (llqqPlateMapping == null)
            {
                return NotFound();
            }
            return View(llqqPlateMapping);
        }

        // POST: PlateRegistration/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        public async Task<IActionResult> Edit(int id, [Bind("ID,UserID,CardID,PlateNo,Deleted")] llqqPlateMapping llqqPlateMapping)
        {
            if (id != llqqPlateMapping.ID)
            {
                return NotFound();
            }
            if (_context.llqqPlateMapping.Where(a => a.PlateNo == llqqPlateMapping.PlateNo && !a.Deleted && a.ID != llqqPlateMapping.ID).Any())
            {
                ModelState.AddModelError("PlateNo", "Plate No duplicate");
            }
            if (_context.llqqPlateMapping.Where(a => a.CardID == llqqPlateMapping.CardID && !a.Deleted && a.ID != llqqPlateMapping.ID).Any())
            {
                ModelState.AddModelError("CardID", "CardID duplicate");
            }
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(llqqPlateMapping);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!llqqPlateMappingExists(llqqPlateMapping.ID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(llqqPlateMapping);
        }
        [Authorize(AuthenticationSchemes = Startup.AuthSchemes)]
        // GET: PlateRegistration/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var llqqPlateMapping = await _context.llqqPlateMapping
                .FirstOrDefaultAsync(m => m.ID == id);
            if (llqqPlateMapping == null)
            {
                return NotFound();
            }

            return View(llqqPlateMapping);
        }

        // POST: PlateRegistration/Delete/5
        [HttpPost, ActionName("Delete")]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var llqqPlateMapping = await _context.llqqPlateMapping.FindAsync(id);
            _context.llqqPlateMapping.Remove(llqqPlateMapping);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool llqqPlateMappingExists(int id)
        {
            return _context.llqqPlateMapping.Any(e => e.ID == id);
        }
    }
}
